package gr.atc.epart.googlewrapper.accounts;

import gr.atc.epart.googlewrapper.GoogleServiceCallbackType;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class GoogleAccountManager
{
	private AccountManager	_accountManager;
	private Account			_account;
	private Handler 		_ownerCallback;
	private Activity		_activity;


	public GoogleAccountManager(Activity activity)
	{
		_accountManager = AccountManager.get(activity);
		_account = getAccount(_accountManager);
		_activity = activity;
		_ownerCallback = null;
	}

	public GoogleAccountManager(Activity activity,Handler ownerCallback)
	{
		_accountManager = AccountManager.get(activity);
		_account = getAccount(_accountManager);
		_activity = activity;
		_ownerCallback = ownerCallback;
	}

	public String getAccountEmail()
	{
		// this is needed in case the accoutn manager cannot provide
		// authentication tokens for some kind of accoutns (e.g. moderator)
		// and we need to manually perform authentication.
		if (_account!=null)
		{
			return _account.name;
		}
		return null;
	}
	
	public void getModeratorToken()
	{
		 getAuthToken("moderator");
	}
	
	public void invalidateModeratorToken(String authToken)
	{
		_accountManager.invalidateAuthToken("moderator", authToken);
	}
	
	public void getCalendarToken()
	{
		getAuthToken("cl");		
	}

	public void getYouTubeToken()
	{
		getAuthToken("youtube");		
	}
	
	public void getPicassaToken()
	{
		getAuthToken("lh2");
	}


	private void getAuthToken(final String service)
	{
	
		if (_account != null)
		{
			_accountManager.getAuthToken(_account, service, false,
					new AccountManagerCallback<Bundle>()
					{
						public void run(AccountManagerFuture<Bundle> arg0)
						{
							try
							{
								
								Bundle result = arg0.getResult();
								
								if (result.containsKey("authtoken"))
								{
									if (_ownerCallback!=null)
									{
										String authToken  = result.getString("authtoken");

										Message msg = new Message();
										msg.what = GoogleServiceCallbackType.AuthTokenReceived;

										Bundle bundle = new Bundle();
										bundle.putString("AUTHTOKEN",authToken);
										msg.setData(bundle);

										_ownerCallback.sendMessage(msg);
									}
								}
								else
								{
									
									Intent intent = (Intent) result
											.get("intent");
									intent.setFlags(intent.getFlags()
											& ~Intent.FLAG_ACTIVITY_NEW_TASK);
									_activity.startActivityForResult(intent, 0);
									Log.i("acclb", "does not contain token");
									// TODO:: after the user acceptance, the result of the activity should be checked and the 
									// auth token should be returned to the application.
									//_ownerCallback.sendEmptyMessage(GoogleServiceCallbackType.UserAuthenticationRequired);
								}
							} catch (Exception e)
							{
								//Log.e("GoogleAccountManager","An exception occurred", e);
								if (_ownerCallback!=null)
								{
									Message msg = new Message();
									msg.what = GoogleServiceCallbackType.OperationError;

									Bundle bundle = new Bundle();
									bundle.putString("ERROR", e.getMessage());
									msg.setData(bundle);

									_ownerCallback.sendMessage(msg);
								}
							}
						}
					}, null);
		}
		
	}
	
	private Account getAccount(AccountManager accountManager)
	{
		Account[] accounts = accountManager.getAccountsByType("com.google");
		Account account;
		if (accounts.length > 0)
		{
			account = accounts[0];
		}
		else
		{
			account = null;
		}
		return account;
	}


	
}
